package com.wuzhipeng.heart.user.controller;




import com.wuzhipeng.heart.user.convention.result.Result;
import com.wuzhipeng.heart.user.convention.result.Results;

import com.wuzhipeng.heart.user.dao.entity.Therapist;
import com.wuzhipeng.heart.user.dao.entity.Therapistdata;
import com.wuzhipeng.heart.user.dto.request.*;
import com.wuzhipeng.heart.user.service.ITherapistService;
import com.wuzhipeng.heart.user.service.ITherapistdataService;
import com.wuzhipeng.heart.user.service.IUsersService;
import com.wuzhipeng.heart.user.service.impl.TherapistdataServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final IUsersService usersService;
    private final ITherapistService therapistService;
    private final ITherapistdataService therapistdataService;


    /**
     * 用户登录
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody loginReq loginRequest) {
        String token = "";
        if(loginRequest.getUserType().equals("therapist")){
            token = therapistService.login(loginRequest.getPhone(), loginRequest.getPassword());
        }
        else if(loginRequest.getUserType().equals("client"))
        {
            token = usersService.login(loginRequest.getPhone(), loginRequest.getPassword());
        }
        else
        {
            return Results.failure( "1111","用户类型错误");
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        return Results.success(map);
    }

    /**
     * 更新用户信息
     * @param usersDataDTO
     * @return
     */
    @PutMapping(value = "/user/profile")
    public Result updateProfile(@RequestBody UsersDataDTO usersDataDTO) {
        usersService.updateProfile(usersDataDTO);
        return Results.success();
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/user/info")
    public Result getUserInfo(@RequestHeader("Authorization") String token) {
        UsersDataDTO usersDataDTO = usersService.getUserInfo(token);
        return Results.success(usersDataDTO);
    }
    /**
     * 用户注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/auth/register")
    public Result register(@RequestBody registerDTO registerDTO) {
        if(registerDTO.getUserType().equals("client")){
            usersService.registerClient(registerDTO);
        }
        if(registerDTO.getUserType().equals("therapist")){
            therapistService.registerTherapist(registerDTO);
            therapistdataService.registerTherapistdata(registerDTO);
        }

        return Results.success("注册成功");

    }

    /**
     * 更新密码
     * @param passwordDTO
     * @return
     */
    @PutMapping("/user/password")
    public Result updatePassword(@RequestBody PasswordDTO passwordDTO) {
        usersService.updatePassword(passwordDTO);
        return Results.success("密码修改成功");
    }
    /**
     * 获取用户列表
     * @return
     */
    @GetMapping("/stats/overview")
    public Result getOverview() {
        Long clientCount = usersService.count();
        Long therapistCount = therapistService.count();
        Long satisfiedCount = 99L;
        Long consultCount = 1000L;
        StatsOverviewVO statsOverviewVO =StatsOverviewVO.builder()
               .userCount(clientCount)
               .therapistCount(therapistCount.intValue())
               .satisfactionRate(satisfiedCount.intValue())
               .consultationHours(consultCount)
               .build();
        return Results.success(statsOverviewVO,"success");
    }



}
